<?php
// include ImageManipulator class
require_once('../lib/ImageManipulator.php');
$arrRes=array();
if ($_FILES['fileToUpload']['error'] > 0) {
    $arrRes['thongbao'] =  "Error: " . $_FILES['fileToUpload']['error'] . "<br />";
} else {
    // array of valid extensions
    $validExtensions = array('.jpg', '.jpeg', '.gif', '.png');
    // get extension of the uploaded file
    $fileExtension = strrchr($_FILES['fileToUpload']['name'], ".");
    // check if file Extension is on the list of allowed ones
    if (in_array($fileExtension, $validExtensions)) {
        $newNamePrefix = time() . '_';
        $newName = time() . '_' . $_FILES['fileToUpload']['name'];
        
        $manipulator1 = new ImageManipulator($_FILES['fileToUpload']['tmp_name']);
        
        // get width , height image
        $width  = $manipulator1->getWidth();
        $height = $manipulator1->getHeight();
      
        $new_width = 270;
        $percent = round(270*100/$width);
        $new_height = round($percent*$height/100);       
        
        
        $newImage1 = $manipulator1->resample($new_width, $new_height);
        // saving file to uploads folder
        $manipulator1->save('../../upload/blog/r/' . $newNamePrefix . $_FILES['fileToUpload']['name']);
        
        $url_hien = '../upload/blog/r/' . $newNamePrefix . $_FILES['fileToUpload']['name'];       
        
        // upload hinh goc
        $destination = '../../upload/blog/' . $newName;
        move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $destination);
        $arrRes['thongbao'] =  'Upload thành công';
	$arrRes['str_hinhanh']="<input type='hidden' name='url_images' value='".$destination."'>";
        $arrRes['text'] .="<img src='".$url_hien."'/>";
        
    } else {
        $arrRes['thongbao'] =  'You must upload an image...';
    }
}
echo json_encode($arrRes);